import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Gao zhao xu
 * Date: 2023-12-18
 * Time: 上午 08:23
 */
public class Test {
    static class Edge {
        public int srcIndex;
        public int destIndex;
        public int weight;

        public Edge(int srcIndex, int destIndex, int weight) {
            this.srcIndex = srcIndex;
            this.destIndex = destIndex;
            this.weight = weight;
        }
    }
    public static int kruskal(int n, int[][] matrix) {
        //1.定义一个优先级队列，用于存储边
        PriorityQueue<Edge> minHeap = new PriorityQueue<>((o1, o2) -> o1.weight - o2.weight);
        //2.遍历邻接矩阵，把所有的边放到优先级队列中
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                //无向图只需要遍历一半
                if (i < j && matrix[i][j] != Integer.MAX_VALUE) {
                    minHeap.offer(new Edge(i, j, matrix[i][j]));
                }
            }
        }
        int[] elem = new int[n];
        Arrays.fill(elem, -1);
        //3.开始从优先级队列中取边
        int size = 0;
        int totalWeight = 0;
        while (size < n - 1 && !minHeap.isEmpty()) {
            Edge edge = minHeap.poll();
            int srcIndex = edge.srcIndex;
            int destIndex = edge.destIndex;
            if (findRoot(elem,srcIndex) != findRoot(elem,destIndex)) {
                size++; //记录添加的边的条数
                union(elem, srcIndex, destIndex);
                totalWeight += edge.weight;
            }
        }
        if (size == n - 1) return totalWeight;
        return -1; //没有最小生成树
    }
    public static int findRoot (int[] elem, int x) {
        if (x < 0) throw new IndexOutOfBoundsException("下标不合法");
        while (elem[x] >= 0) {
            x = elem[x];
        }
        return x;
    }
    public static void union(int[] elem, int x1, int x2) {
        int x1Root = findRoot(elem, x1);
        int x2Root = findRoot(elem, x2);
        if (x1Root != x2Root) {
            if (x1Root > x2Root) {
                int tmp = x1Root;
                x1Root = x2Root;
                x2Root   = tmp;
            }
            elem[x1Root] += elem[x2Root];
            elem[x2Root] = x1Root;
        }
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int[][] matrix = new int[n][n];
        for (int i = 0; i < n; i++) {
            Arrays.fill(matrix[i], Integer.MAX_VALUE);
        }
        for (int i = 0; i < n - 1; i++) {
            String srcV = input.next();
            int srcIndex = srcV.charAt(0) - 'A';
            int x = input.nextInt();
            for (int j = 0; j < x; j++) {
                String destV = input.next();
                int destIndex = destV.charAt(0) - 'A';
                int weight = input.nextInt();
                matrix[srcIndex][destIndex] = weight;
                matrix[destIndex][srcIndex] = weight;
            }
        }
        System.out.println(kruskal(n, matrix));
    }
}
